home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / PropertiesMetalTheme.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  186 lines

  1. /* 
  2.  * @(#)PropertiesMetalTheme.java    1.1 98/03/11
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21.  
  22. import com.sun.java.swing.plaf.*;
  23. import com.sun.java.swing.plaf.metal.*;
  24. import com.sun.java.swing.*;
  25. import com.sun.java.swing.border.*;
  26. import java.awt.*;
  27. import java.io.*;
  28. import java.util.*;
  29.  
  30. /**
  31.  * This class allows you to load a theme from a file.
  32.  * It uses the standard Java Properties file format.
  33.  * To create a theme you provide a text file which contains
  34.  * tags corresponding to colors of the theme along with a value
  35.  * for that color.  For example:
  36.  *
  37.  * name=My Ugly Theme
  38.  * primary1=255,0,0
  39.  * primary2=0,255,0
  40.  * primary3=0,0,255
  41.  *
  42.  * This class only loads colors from the properties file,
  43.  * but it could easily be extended to load fonts -  or even icons.
  44.  *
  45.  * @version 1.1 03/11/98
  46.  * @author Steve Wilson
  47.  */
  48. public class PropertiesMetalTheme extends DefaultMetalTheme {
  49.  
  50.     private String name = "Custom Theme";
  51.  
  52.     private ColorUIResource primary1;
  53.     private ColorUIResource primary2;
  54.     private ColorUIResource primary3;
  55.  
  56.     private ColorUIResource secondary1;
  57.     private ColorUIResource secondary2;
  58.     private ColorUIResource secondary3;
  59.  
  60.     private ColorUIResource black;
  61.     private ColorUIResource white;
  62.  
  63.  
  64.     /**
  65.       * pass an inputstream pointing to a properties file.
  66.       * Colors will be initialized to be the same as the DefaultMetalTheme,
  67.       * and then any colors provided in the properties file will override that.
  68.       */
  69.     public PropertiesMetalTheme( InputStream stream ) {
  70.         initColors();
  71.         loadProperties(stream);
  72.     }
  73.  
  74.     /**
  75.       * Initialize all colors to be the same as the DefaultMetalTheme.
  76.       */
  77.     private void initColors() {
  78.         primary1 = super.getPrimary1();
  79.         primary2 = super.getPrimary2();
  80.         primary3 = super.getPrimary3();
  81.  
  82.         secondary1 = super.getSecondary1();
  83.         secondary2 = super.getSecondary2();
  84.         secondary3 = super.getSecondary3();
  85.  
  86.     black = super.getBlack();
  87.     white = super.getWhite();
  88.     }
  89.  
  90.     /**
  91.       * Load the theme name and colors from the properties file
  92.       * Items not defined in the properties file are ignored
  93.       */
  94.     private void loadProperties(InputStream stream) {
  95.     Properties prop = new Properties();
  96.     try {
  97.         prop.load(stream);
  98.     } catch (IOException e) {
  99.         System.out.println(e);
  100.     }
  101.  
  102.     Object tempName = prop.get("name");
  103.     if (tempName != null) {
  104.         name = tempName.toString();
  105.     }
  106.  
  107.     Object colorString = null;
  108.  
  109.     colorString = prop.get("primary1");
  110.     if (colorString != null){
  111.         primary1 = parseColor(colorString.toString());
  112.     }
  113.  
  114.     colorString = prop.get("primary2");
  115.     if (colorString != null) {
  116.         primary2 = parseColor(colorString.toString());
  117.     }
  118.  
  119.     colorString = prop.get("primary3");
  120.     if (colorString != null) {
  121.         primary3 = parseColor(colorString.toString());
  122.     }
  123.  
  124.     colorString = prop.get("secondary1");
  125.     if (colorString != null) {
  126.         secondary1 = parseColor(colorString.toString());
  127.     }
  128.  
  129.     colorString = prop.get("secondary2");
  130.     if (colorString != null) {
  131.         secondary2 = parseColor(colorString.toString());
  132.     }
  133.  
  134.     colorString = prop.get("secondary3");
  135.     if (colorString != null) {
  136.         secondary3 = parseColor(colorString.toString());
  137.     }
  138.  
  139.     colorString = prop.get("black");
  140.     if (colorString != null) {
  141.         black = parseColor(colorString.toString());
  142.     }
  143.  
  144.     colorString = prop.get("white");
  145.     if (colorString != null) {
  146.         white = parseColor(colorString.toString());
  147.     }
  148.  
  149.     }
  150.  
  151.     public String getName() { return name; }
  152.  
  153.     protected ColorUIResource getPrimary1() { return primary1; }
  154.     protected ColorUIResource getPrimary2() { return primary2; }
  155.     protected ColorUIResource getPrimary3() { return primary3; }
  156.  
  157.     protected ColorUIResource getSecondary1() { return secondary1; }
  158.     protected ColorUIResource getSecondary2() { return secondary2; }
  159.     protected ColorUIResource getSecondary3() { return secondary3; }
  160.  
  161.     protected ColorUIResource getBlack() { return black; }
  162.     protected ColorUIResource getWhite() { return white; }
  163.  
  164.     /**
  165.       * parse a comma delimited list of 3 strings into a Color
  166.       */
  167.     private ColorUIResource parseColor(String s) {
  168.         int red = 0;
  169.     int green = 0;
  170.     int blue = 0;
  171.     try {
  172.         StringTokenizer st = new StringTokenizer(s, ",");
  173.  
  174.         red = Integer.parseInt(st.nextToken());
  175.         green = Integer.parseInt(st.nextToken());
  176.         blue = Integer.parseInt(st.nextToken());
  177.  
  178.     } catch (Exception e) {
  179.         System.out.println(e);
  180.         System.out.println("Couldn't parse color :" + s);
  181.     }
  182.     
  183.     return new ColorUIResource(red, blue, green);
  184.     }
  185. }
  186.